const menuData = [{
    title: '推广中心',
    tit: '',
    url: '/pages/usercenter/promotion',
    img: '/assets/tgzx1.png',
  },

  {
    title: '地址管理',
    tit: '',
    url: '/pages/usercenter/address/list/index',
    img: '/assets/addr1.png',
  },
  {
    title: '平台客服',
    tit: '',
    url: '',
    type: 'kf',
    img: '/assets/ptkf1.png',
  },
  {
    title: '实名认证',
    tit: '',
    url: '/pages/usercenter/identity',
    type: 'identity',
    img: '/assets/smrz1.png',
  },
  {
    title: '隐私协议',
    tit: '',
    url: '/pages/web/html?type=1',
    type: '',
    img: '/assets/ysxy.png',
  },
  {
    title: '用户协议',
    tit: '',
    url: '/pages/web/html?type=2',
    type: '',
    img: '/assets/yhxy.png',
  },
  {
    title: '退出登陆',
    tit: '',
    url: '/pages/login/index',
    type: 'logout',
    img: '/assets/tcdl1.png',
  },

];

const orderTagInfos = [{
    title: '预订单',
    iconName: 'wallet',
    orderNum: 0,
    tabType: 999,
    status: 1,
    img: '/assets/ydd.png'
  },
  {
    title: '待取货',
    iconName: 'deliver',
    orderNum: 0,
    tabType: 1,
    status: 1,
    img: '/assets/dqh.png'
  },
  {
    title: '待评价',
    iconName: 'package',
    orderNum: 0,
    tabType: 5,
    status: 1,
    img: '/assets/dpj.png'
  },
  {
    title: '已完成',
    iconName: 'comment',
    orderNum: 0,
    tabType: 7,
    status: 1,
    img: '/assets/ywc.png'
  },
  {
    title: '退款/售后',
    iconName: 'exchang',
    orderNum: 0,
    tabType: 8,
    status: 1,
    img: '/assets/tksh.png'
  },
];

const getDefaultData = () => ({
  showMakePhone: false,
  userInfo: {
    avatarUrl: '',
    nickName: '正在登录...',
    phoneNumber: '',
  },
  menuData,
  orderTagInfos,
  customerServiceInfo: {},
  /**1未登录的情况; 2已登录但未授权用户信息情况; 3已登录且已经授权用户信息的情况 */
  currAuthStep: 1,
  /**是否显示我的门店 */
  showStore: false,
});

const basePage = require('../../common/BasePage')
Page({
  behaviors: [basePage],
  data: getDefaultData(),

  onLoad() {
    let userInfo = wx.getStorageSync('userInfo');

    if (userInfo.roleKeySet.indexOf("store_manager") >= 0 || userInfo.roleKeySet.indexOf("shop_assistant") >= 0) {
      this.data.menuData.splice(1, 0, {
        title: '我的门店',
        tit: '',
        url: '/pages/store/index',
        img: '/assets/wdmd1.png',
      })
      this.setData({
        menuData: this.data.menuData
      })
    }
    // console.log(wx.getStorageSync('Authorization'));
  },

  onShow() {
    this.getTabBar().init();
    this.init();

  },
  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.fetUseriInfoHandle();
    // this.req_coupons();
    ///v1/coupons/me
  },

  fetUseriInfoHandle() {
    this.request({
      url: this.data.host + "/v1/users/me"
    }, res => {

      getApp().globalData.userInfo = res.data;
      if (res.data.avatar) {
        res.data.avatarUrl = this.get_imgUrl(res.data.avatar)
      }
      this.setData({
        userInfo: res.data,
        currAuthStep: res.data.nickName ? 3 : 2
        // nickName
      })
    })

  },

  menu_click(e) {
    console.log('menu_click', e);
    let item = e.currentTarget.dataset.item;

    if (item.type == 'kf') {
      this.request({
        url: this.data.host + "/v1/limits/consumer",
        loadingTip: '获取客服...'
      }, res => {
        console.log('客服', res.data);
        wx.openCustomerServiceChat({
          corpId: 'ww72ede85652b45a26',
          extInfo: {
            url: res.data,
          },
          success: (res) => {
            console.log('打开成功', res);

          },
          fail: (res) => {
            console.log('打开失败', res);
            wx.showToast({
              title: '打开失败',
              icon: "error"
            })
          },

        })
      })

    } else if (item.type == 'logout') {
      wx.showModal({
        content: '确定要退出登录吗?',
        success: (result) => {
          if (result.confirm) {
            this.request({
              url: this.data.host + "/v1/authentication/logout",
              method: "delete"
            }, res => {
              //wx.getStorageSync('Authorization');
              wx.removeStorage({
                key: 'Authorization',
              })
            }, err => {

            })
            // wx.removeStorage({
            //   key: 'Authorization',
            // })
            wx.navigateTo({
              url: item.url,
            })
          }
        },
        fail: (res) => {},
        complete: (res) => {},
      })
    } else if (item.url) {
      wx.navigateTo({
        url: item.url + "?cardNumber=" + this.data.userInfo.cardNumber,

      })
    }
  },

  jumpNav(e) {
    const status = e.detail.tabType;

    if (status === 0) {
      wx.navigateTo({
        url: '/pages/order/after-service-list/index'
      });
    } else {
      wx.navigateTo({
        url: `/pages/order/list?status=${status}`
      });
    }
  },

  jumpAllOrder() {
    wx.navigateTo({
      url: '/pages/order/list'
    });
  },

  openMakePhone() {
    this.setData({
      showMakePhone: true
    });
  },
  closeMakePhone() {
    this.setData({
      showMakePhone: false
    });
  },

  call() {
    wx.makePhoneCall({
      phoneNumber: this.data.customerServiceInfo.servicePhone,
    });
  },

  gotoUserEditPage() {
    const {
      currAuthStep
    } = this.data;
    if (currAuthStep === 2) {
      wx.navigateTo({
        url: '/pages/usercenter/person-info/index'
      });
    } else {
      this.fetUseriInfoHandle();
    }
  },


});